import { ECSModel } from "./ECSModel";

export class ECSMask{
    private mask:Uint32Array;
    private size:number = 0;
    constructor(){
        let length = Math.ceil(ECSModel.compTid / 31);
        this.mask = new Uint32Array(length);
        this.size = length
    }
    public set(num:number):void{
        this.mask[((num / 31)>>>0)] |= (1 <<(num %31))
    }

    public delete(num:number):void{
        this.mask[((num/31) >>>0)]&= ~(1<<(num%31))
    }
    public has (num:number):boolean{
        return !!(this.mask[((num/31) >>>0)] & (1 <<(num % 31))) 
    }

    public or (other:ECSMask):boolean{

        for(let i =0 ; i < this.size;i++){
            if (this.mask[i] & other.mask[i]) {
                return true;
            }
        }
        return false;
    }

    public and(other:ECSMask):boolean{
        for(let i = 0; i < this.size;i++){
            if ((this.mask[i] & other.mask[i])!= this.mask[i]) {
                return false;
            }
        }
        return true;
    }

    public clear():void{
        for(let i =0 ; i < this.size;i++){
            this.mask[i] = 0;
        }
    }

}